/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GestionBaseDatos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public final class AdministradorConexion {

    private Connection conexion;
    private static String host="localhost";
    private static String db="umbook";
    private static String user="root";
    private static String pass="root";

    public static void iniciar(String host, String db, String user, String pass) {

        AdministradorConexion.host = host;
        AdministradorConexion.db = db;
        AdministradorConexion.user = user;
        AdministradorConexion.pass = pass;

    }


    
    private AdministradorConexion() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

               conexion = DriverManager.getConnection("jdbc:mysql://"+ host + "/"+ db ,user,pass);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public Connection getConexion() {
        return conexion;
    }

    public void cerrarConexion() {
        try {
            conexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(AdministradorConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static AdministradorConexion instancia;

    public static AdministradorConexion getInstancia() {
        if (instancia == null) {
            instancia = new AdministradorConexion();
        }

        return instancia;
    }

        public int ejecutar(String consulta) throws  SQLException{
        Statement s = conexion.createStatement();
        return s.executeUpdate(consulta);
    }


     public ResultSet consultar(String consulta) throws SQLException{
        Statement s = conexion.createStatement();
        return s.executeQuery(consulta);
    }

    public PreparedStatement preparar(String consulta) throws SQLException{
        return conexion.prepareStatement(consulta);

    }

}
